package com.doit.kafkaday01;

import org.apache.kafka.clients.admin.*;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.sql.*;
import java.time.Duration;
import java.util.*;

public class _03_kafka2mysql {
    public static void main(String[] args) throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "123456");
        conn.setAutoCommit(false);
        PreparedStatement pps = conn.prepareStatement("insert into user_info values(?,?,?,?)");
        PreparedStatement off_pps = conn.prepareStatement("insert into off values (?,?,?,?) on duplicate key update `offset` = ?;");
        PreparedStatement get_off = conn.prepareStatement("select `partition`,`offset` from off where group_id = ? and topic = ?");

        Properties props = new Properties();
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"group003");
//        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"true");
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
        //订阅主题
//        consumer.subscribe(Arrays.asList("mysql"));

        //从上次消费到的地方开始读   我上次消费到的地方在哪？   __consumer_offsets里面吗
        //去mysql数据库中找上次消费到了哪里啊  告诉他，要从哪里开始消费
        get_off.setString(1,"group003");
        get_off.setString(2,"mysql");
        ResultSet resultSet = get_off.executeQuery();
        Properties props1 = new Properties();
        props1.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");

        //获取该topic的详细信息。来确定kafkatopic的分区个数
        AdminClient adminClient = KafkaAdminClient.create(props1);
        DescribeTopicsResult mysql = adminClient.describeTopics(Arrays.asList("mysql"));
        Map<String, KafkaFuture<TopicDescription>> values = mysql.values();

        HashMap<TopicPartition, Long> map = new HashMap<>();
        for (int i = 0; i < values.size(); i++) {
            TopicPartition topicPartition = new TopicPartition("mysql", i);
            map.put(topicPartition,0L);
        }

        /**
         * 如果我mysql里面记录的分区不全怎么办  就可能一个分区都没有设置，或者只设置了一两个分区，而我又很多分区
         */
        while (resultSet.next()){
            int partition = resultSet.getInt("partition");
            long offset = resultSet.getLong("offset");
            //再程序中指定从哪一个分区的那一个offset开始读取数据
            TopicPartition topicPartition = new TopicPartition("mysql", partition);
            map.put(topicPartition,offset+1);
        }

        Set<TopicPartition> topicPartitions = map.keySet();
        consumer.assign(topicPartitions);
        Set<Map.Entry<TopicPartition, Long>> entries = map.entrySet();
        for (Map.Entry<TopicPartition, Long> entry : entries) {
            TopicPartition key = entry.getKey();
            Long value = entry.getValue();
            consumer.seek(key,value);
        }


        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));

            for (ConsumerRecord<String, String> record : poll) {
                try {
                    //1,zss,18,male
                    String value = record.value();
                    String[] arr = value.split(",");
                    int id = Integer.parseInt(arr[0]);
                    String name = arr[1];
                    int age = Integer.parseInt(arr[2]);
                    String gender = arr[3];
                    //将4个字段的值写入到mysql中
                    pps.setInt(1,id);
                    pps.setString(2,name);
                    pps.setInt(3,age);
                    pps.setString(4,gender);
                    pps.execute();

                    //偏移量的提交我要提交到mysql中 既然是写到一张表里面，表结构怎么设计呢？  组id   主题   分区   偏移量   __consumer_offsets这个topic里面存的是什么，我们就学他呗
                    String topic = record.topic();
                    int partition = record.partition();
                    long offset = record.offset();

                    off_pps.setString(1,"group003");
                    off_pps.setString(2,topic);
                    off_pps.setInt(3,partition);
                    off_pps.setLong(4,offset);
                    off_pps.setLong(5,offset);
                    off_pps.execute();

                    //提交事务
                    conn.commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    //回滚事务
                    conn.rollback();
                }


            }

        }



    }
}
